#include "tree.h"

bool start( char *parse_fn, char *out_fn )
{
	CUTextFile parse_file( parse_fn, "r" );
	if( parse_file.IsOpen() == false )	return false;

	CUTextFile out_file( out_fn, "w" );
	if( out_file.IsOpen() == false )	return false;

	int now_line=0;
	while(1)	{
		CUString line;
		if( parse_file.ReadLine( line ) == false ) break;
		now_line++;


		CTree t ;
		t.Init();
		if( t.LoadTree( line ) == false )	{
			fprintf( stderr, "load fail: %d\n", now_line );
			fprintf( out_file.GetFP(), "INVALID TREE\n" );
			fflush( out_file.GetFP() );
			continue;
		}


		int ls[NUM_MAX_NODE];
		int s;

		t.GetLexNodeList( ls, s, ROOT_ID );
		for( int i=0; i<s; i++ )	{
			CNode &n = t.node[ ls[i] ];
			CNode &p = t.node[ n.parent ];

			if( i > 0 )	{
				fprintf( out_file.GetFP(), " " );
				fflush( out_file.GetFP() );
			}
			fprintf( out_file.GetFP(), "%s/%s", n.label.GetStr(), p.label.GetStr()  );
			fflush( out_file.GetFP() );
		}
		fprintf( out_file.GetFP(), "\n" );
		fflush( out_file.GetFP() );
	}



	return true;
}


int main( int argc, char *argv[] )
{
	if( argc != 3 )	{
		fprintf( stderr, "usage: parse2pos parse-file  pos-file\n" );
		return 1;
	}

	if( start( argv[1], argv[2] ) == false )	{
		return 1;
	}

	return 0;
}


